#include "addmodifystaffinformation.h"
#include "ui_addmodifystaffinformation.h"
#include "calendar.h"
#include "inquiresql.h"
#include <QDebug>
#include <QKeyEvent>
#include <QMessageBox>
#include "personstaffinformation.h"
#include <QDateTime>

AddModifyStaffInformation::AddModifyStaffInformation(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AddModifyStaffInformation)
{
    ui->setupUi(this);
    this->setLayout(ui->MainLayout);
    this->setFixedSize(518, 532);

    //设置自定义的鼠标指针图案
    QCursor *myCursor= new QCursor(QPixmap(":/Mouse/Image/mouse/mouse.png"),15,10);
    setCursor(*myCursor);

    m_lastClick = -1;
    m_LineEditDate = -1;

    m_calendar = new Calendar(this);
    m_calendar->hide();

    QRegExp regExpPrice(" [0-9]{0,6}|[0-9]{0,6}[.]{1}[0-9]{0,2} ");
    QRegExpValidator *pRegExpValidatorPrice = new QRegExpValidator(regExpPrice, this);
    ui->lineEditWage->setValidator(pRegExpValidatorPrice);

    QRegExp regExpMinStock("[0-9]{1,3}");
    QRegExpValidator *pRegExpValidatorMinStock = new QRegExpValidator(regExpMinStock, this);
    ui->lineEditAge->setValidator(pRegExpValidatorMinStock);

    QRegExp regExpStaffID("[0-9]{1,6}");
    QRegExpValidator *pRegExpValidatorStaffID = new QRegExpValidator(regExpStaffID, this);
    ui->lineEditStaffID->setValidator(pRegExpValidatorStaffID);

    connect(m_calendar, SIGNAL(signalUpdateLineEditDate(QString)), this, SLOT(slotUpdateLineEditDate(QString)));

    //添加事件过滤器
    ui->lineEditStaffID->installEventFilter(this);
    ui->lineEditName->installEventFilter(this);
    ui->comboBoxGender->installEventFilter(this);
    ui->lineEditBirthday->installEventFilter(this);
    ui->lineEditEntryDate->installEventFilter(this);
    ui->lineEditAge->installEventFilter(this);
    ui->lineEditWage->installEventFilter(this);
    ui->lineEditTel->installEventFilter(this);
    ui->lineEditAddress->installEventFilter(this);
    ui->textEditRemark->installEventFilter(this);
    ui->pushButtonSave->installEventFilter(this);
}

AddModifyStaffInformation::~AddModifyStaffInformation()
{
    delete ui;
}

bool AddModifyStaffInformation::eventFilter(QObject *target, QEvent *event)
{
    if (       target == ui->lineEditStaffID   || target == ui->lineEditName
            || target == ui->comboBoxGender    || target == ui->lineEditBirthday
            || target == ui->lineEditEntryDate || target == ui->lineEditAge
            || target == ui->lineEditWage      || target == ui->lineEditTel
            || target == ui->lineEditAddress   || target == ui->textEditRemark
            || target == ui->pushButtonSave    )
    {
        if (event->type() == QEvent::FocusOut)
        {
            if (target == ui->lineEditWage)
            {
                if (ui->lineEditWage->text().isEmpty()
                        || ui->lineEditWage->text().toDouble() == 0)
                {
                    ui->lineEditWage->setText("0.00");
                    return true;
                }

                if (ui->lineEditWage->text().contains(".", Qt::CaseInsensitive))
                {
                    if (ui->lineEditWage->text().left(1) == ".")
                    {
                        ui->lineEditWage->setText(ui->lineEditWage->text().replace(0, 1, "0."));
                        return true;
                    }
                    if (ui->lineEditWage->text().right(1) == ".")
                    {
                        ui->lineEditWage->setText(ui->lineEditWage->text() + "00");
                        return true;
                    }
                    if (ui->lineEditWage->text().right(2).contains(".", Qt::CaseInsensitive))
                        ui->lineEditWage->setText(ui->lineEditWage->text() + "0");
                }
                else
                {
                    ui->lineEditWage->setText(ui->lineEditWage->text() + ".00");
                    return true;
                }
            }
        }
        //鼠标左键按下事件
        if (event->type() == QEvent::MouseButtonPress)
        {
            QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
            if (mouseEvent->button() == Qt::LeftButton)
            {
                if (target == ui->lineEditStaffID)
                {
                    if (m_lastClick != lineEditStaffID){
                        m_lastClick = lineEditStaffID;
                        ui->lineEditStaffID->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditStaffID)
                    {
                        goto flag;
                    }
                }
                if (target == ui->lineEditName)
                {
                    if (m_lastClick != lineEditName){
                        m_lastClick = lineEditName;
                        ui->lineEditName->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditName)
                    {
                        goto flag;
                    }
                }
                if (target == ui->comboBoxGender)
                {
                    ui->comboBoxGender->setCurrentIndex(-1);
                    return true;
                }
                if (target == ui->lineEditBirthday)
                {
                    if (m_lastClick != lineEditBirthday){
                        m_lastClick = lineEditBirthday;
                        ui->lineEditBirthday->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditBirthday)
                    {
                        goto flag;
                    }
                }
                if (target == ui->lineEditEntryDate)
                {
                    if (m_lastClick != lineEditEntryDate){
                        m_lastClick = lineEditEntryDate;
                        ui->lineEditEntryDate->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditEntryDate)
                    {
                        goto flag;
                    }
                }
                if (target == ui->lineEditAge)
                {
                    if (m_lastClick != lineEditAge){
                        m_lastClick = lineEditAge;
                        ui->lineEditAge->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditAge)
                    {
                        goto flag;
                    }
                }
                if (target == ui->lineEditWage)
                {
                    if (m_lastClick != lineEditWage){
                        m_lastClick = lineEditWage;
                        ui->lineEditWage->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditWage)
                    {
                        goto flag;
                    }
                }
                if (target == ui->lineEditTel)
                {
                    if (m_lastClick != lineEditTel){
                        m_lastClick = lineEditTel;
                        ui->lineEditTel->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditTel)
                    {
                        goto flag;
                    }
                }
                if (target == ui->lineEditAddress)
                {
                    if (m_lastClick != lineEditAddress){
                        m_lastClick = lineEditAddress;
                        ui->lineEditAddress->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditAddress)
                    {
                        goto flag;
                    }
                }
                if (target == ui->textEditRemark)
                {
                    if (m_lastClick != textEditRemark){
                        m_lastClick = textEditRemark;
                        ui->textEditRemark->selectAll();
                        return true;
                    }else if (m_lastClick == textEditRemark)
                    {
                        goto flag;
                    }
                }
                if (target == ui->pushButtonSave)
                {
                    m_lastClick = pushButtonSave;
                    goto flag;
                }
            }
        }

        //鼠标左键释放事件
        if (event->type() == QEvent::MouseButtonRelease)
        {
            QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
            if (mouseEvent->button() == Qt::LeftButton)
            {
                if (target == ui->comboBoxGender)
                {
                    ui->comboBoxGender->showPopup();
                    return true;
                }

                if (target == ui->lineEditBirthday)
                {
                    m_LineEditDate = lineEditBirthday;
                    m_calendar->setSelectedCurrentDate(ui->lineEditBirthday->text());
                    m_calendar->setWindowFlags(
                                Qt::Dialog | Qt::FramelessWindowHint);
                    QPoint GlobalPoint(ui->lineEditBirthday->mapToGlobal(QPoint(0, 0)));
                    int x = GlobalPoint.x();
                    int y = GlobalPoint.y();
                    m_calendar->setGeometry(x , y + 35, 150, 130);

                    m_calendar->show();
                    goto flag;
                }
                if (target == ui->lineEditEntryDate)
                {
                    m_LineEditDate = lineEditEntryDate;
                    m_calendar->setSelectedCurrentDate(ui->lineEditEntryDate->text());
                    m_calendar->setWindowFlags(
                                Qt::Dialog | Qt::FramelessWindowHint);

                    QPoint GlobalPoint(ui->lineEditEntryDate->mapToGlobal(QPoint(0, 0)));
                    int x = GlobalPoint.x();
                    int y = GlobalPoint.y();
                    m_calendar->setGeometry(x , y + 35, 150, 130);
                    m_calendar->show();
                    goto flag;
                }
            }
        }

        //键盘事件
        if (event->type() == QEvent::KeyPress)
        {
            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
            if (keyEvent->key() == Qt::Key_Escape)
            {
                this->hide();
                return true;
            }
            if ((keyEvent->key() == Qt::Key_Enter) || (keyEvent->key() == Qt::Key_Return))
            {
                if (target == ui->lineEditStaffID)
                {
                    ui->lineEditName->setFocus();
                    ui->lineEditName->selectAll();
                    return true;
                }
                if (target == ui->lineEditName)
                {
                    ui->comboBoxGender->setFocus();
                    return true;
                }
                if (target == ui->comboBoxGender)
                {
                    ui->lineEditBirthday->setFocus();
                    return true;
                }
                if (target == ui->lineEditBirthday)
                {
                    ui->lineEditEntryDate->setFocus();
                    return true;
                }
                if (target == ui->lineEditEntryDate)
                {
                    ui->lineEditAge->setFocus();
                    ui->lineEditAge->selectAll();
                    return true;
                }
                if (target == ui->lineEditAge)
                {
                    ui->lineEditWage->setFocus();
                    ui->lineEditWage->selectAll();
                    return true;
                }
                if (target == ui->lineEditWage)
                {
                    ui->lineEditTel->setFocus();
                    ui->lineEditTel->selectAll();
                    return true;
                }
                if (target == ui->lineEditTel)
                {
                    ui->lineEditAddress->setFocus();
                    ui->lineEditAddress->selectAll();
                    return true;
                }
                if (target == ui->lineEditAddress)
                {
                    ui->textEditRemark->setFocus();
                    ui->textEditRemark->selectAll();
                    return true;
                }
                if (target == ui->textEditRemark)
                {
                    ui->pushButtonSave->setFocus();
                    on_pushButtonSave_clicked();
                    //保存操作
                    return true;
                }
            }
        }
    }
flag:
    return QWidget::eventFilter(target, event);
}

void AddModifyStaffInformation::slotDisplayAddModifyStaffInformation(int page, int staffID)
{
    m_staffID = staffID;
    m_list = m_sql->InquireStaffInformationList();
    clearStaffInformation();

    if (page == addStaffInformationPage)
    {
        setWindowTitle(tr("新增员工信息"));
        currentPage = addStaffInformationPage;
        ui->checkBoxAdd->show();
//        QString staffAge = QString::number(
//                    QDateTime::currentDateTime().toString("yyyy").toInt()
//                    - ui->lineEditBirthday->text().left(4).toInt());
        ui->lineEditBirthday->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd"));
        ui->lineEditEntryDate->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd"));
        ui->lineEditWage->setText("0.00");
        ui->lineEditAge->setText("0");
    }
    else if(page == modifyStaffInformationPage)
    {
        setWindowTitle(tr("修改员工信息"));
        currentPage = modifyStaffInformationPage;
        ui->checkBoxAdd->hide();

        //显示当前供货商信息
        for(int i = 0; i < m_list.count(); ++i)
        {
            if (m_list.at(i).get_personID().toInt() == staffID)
            {
                ui->lineEditStaffID->setText(m_list.at(i).get_personID());
                ui->lineEditName->setText(m_list.at(i).get_personName());

                if (m_list.at(i).get_personGender() == QString(tr("男性")))
                {
                    ui->comboBoxGender->setCurrentIndex(0);
                }
                else
                    ui->comboBoxGender->setCurrentIndex(1);

                ui->lineEditBirthday->setText(m_list.at(i).get_personBirthday());
                ui->lineEditEntryDate->setText(m_list.at(i).get_entryDate());
                ui->lineEditAge->setText(m_list.at(i).get_personAge());
                ui->lineEditWage->setText(m_list.at(i).get_wage());
                ui->lineEditTel->setText(m_list.at(i).get_personTel());
                ui->lineEditAddress->setText(m_list.at(i).get_personAdress());
                ui->textEditRemark->setText(m_list.at(i).get_remark());
                break;
            }
        }
    }
    ui->lineEditStaffID->setFocus();
    ui->lineEditStaffID->selectAll();
}

void AddModifyStaffInformation::on_pushButtonCancel_clicked()
{
    this->hide();
}

void AddModifyStaffInformation::clearStaffInformation()
{
    ui->lineEditStaffID->setText(NULL);
    ui->lineEditName->setText(NULL);
    ui->comboBoxGender->setCurrentIndex(-1);
    ui->lineEditBirthday->setText(NULL);
    ui->lineEditEntryDate->setText(NULL);
    ui->lineEditAge->setText(NULL);
    ui->lineEditWage->setText(NULL);
    ui->lineEditTel->setText(NULL);
    ui->lineEditAddress->setText(NULL);
    ui->textEditRemark->setText(NULL);
}

bool AddModifyStaffInformation::staffInformationIsEmpty(QString staffInformation, QString msg)
{
    if (staffInformation.isEmpty())
    {
        QMessageBox::information(this, tr("(☆_☆)温馨提示 ..."),
                             "\n"+msg );
        return true;
    }
    return false;
}

void AddModifyStaffInformation::on_pushButtonSave_clicked()
{
    //判断员工信息是否为空
    if (staffInformationIsEmpty(ui->lineEditStaffID->text(), tr("员工ID不能为空")))
        return;
    if (staffInformationIsEmpty(ui->lineEditName->text(), tr("员工姓名不能为空")))
        return;
    if (staffInformationIsEmpty(ui->comboBoxGender->currentText(), tr("员工性别不能为空")))
        return;
    if (staffInformationIsEmpty(ui->lineEditTel->text(), tr("员工电话不能为空")))
        return;

    QString staffAge = QString::number(
                QDateTime::currentDateTime().toString("yyyy").toInt()
                - ui->lineEditBirthday->text().left(4).toInt());
    ui->lineEditAge->setText(staffAge);

    PersonStaffInformation personStaffInformation(ui->lineEditStaffID->text(),
                        ui->lineEditName->text(),
                        ui->comboBoxGender->currentText(),
                        ui->lineEditBirthday->text(),
                        ui->lineEditEntryDate->text(),
                        ui->lineEditAge->text(),
                        ui->lineEditWage->text(),
                        ui->lineEditTel->text(),
                        ui->lineEditAddress->text(),
                        ui->textEditRemark->toPlainText());

    //向数据库中插入员工信息
    if (currentPage == addStaffInformationPage)
    {
        if (!(m_sql->InsertStaffInformation(personStaffInformation)))
        {
            QMessageBox::warning(this, tr(" 警告！"), tr("插入员工信息失败") );
            return ;
        }

        if (!ui->checkBoxAdd->isChecked())
            this->hide();
        else
        {
            clearStaffInformation();
            ui->lineEditStaffID->setFocus();
        }
    }
    if (currentPage == modifyStaffInformationPage)
    {
//        QString::number(staffID).rightJustified(6, '0')  m_staffID
        if (!(m_sql->UpdateStaffInformation(
                  QString::number(m_staffID).rightJustified(6, '0'),
                  personStaffInformation)))
        {
            QMessageBox::critical(this, tr(" 警告！"), tr("修改员工信息失败") );
            return ;
        }
        this->hide();
    }
    emit signalDisplayStaffInformation();
}

void AddModifyStaffInformation::on_pushButtonAutoAddStaffID_clicked()
{
    int staffID = m_list.at(m_list.count() - 1).get_personID().toInt() + 1;
    ui->lineEditStaffID->setText(QString::number(staffID).rightJustified(6, '0'));
}

void AddModifyStaffInformation::slotUpdateLineEditDate(QString date)
{
    switch (m_LineEditDate)
    {
        case lineEditBirthday:
        {
            ui->lineEditBirthday->setText(date);
            QString staffAge = QString::number(
                        QDateTime::currentDateTime().toString("yyyy").toInt()
                        - ui->lineEditBirthday->text().left(4).toInt());
            ui->lineEditAge->setText(staffAge);
            break;
        }
        case lineEditEntryDate:
            ui->lineEditEntryDate->setText(date);
            break;
        default:
            qDebug() << "error";
            break;
    }
}
